package com.hopu.petshop.controller.old;//package com.hopu.petshop.controller;
//
//import com.hopu.petshop.entity.Pet;
//import com.hopu.petshop.service.PetService;
//import org.apache.commons.fileupload.FileItemIterator;
//import org.apache.commons.fileupload.FileItemStream;
//import org.apache.commons.fileupload.FileUploadException;
//import org.apache.commons.fileupload.servlet.ServletFileUpload;
//import org.apache.commons.fileupload.util.Streams;
//import org.apache.commons.io.FilenameUtils;
//
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.OutputStream;
//import java.sql.SQLException;
//
///**
// * 添加宠物的Servlet
// */
//@WebServlet("/addPet.do")
//public class AddPetServlet extends HttpServlet {
//
//    public static final String PATH = "D:\\apache-tomcat-8.5.39\\webapps\\pet-images\\";
//
//    //用spring创建service对象
//    private PetService petService = SpringConfig.getContext().getBean(PetService.class);
//
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //创建上传对象
//        ServletFileUpload upload = new ServletFileUpload();
//        //判断请求中是否有上传内容
//        if(ServletFileUpload.isMultipartContent(req)){
//            //设置上传的大小
//            upload.setFileSizeMax(10 * 1024 * 1024);
//            //获得表单的内容迭代器
//            try {
//                FileItemIterator itemIterator = upload.getItemIterator(req);
//                String name = null;
//                String age = null;
//                String gender = null;
//                String color = null;
//                String price = null;
//                String typeId = null;
//                String image = null;
//                //遍历表单内容
//                while(itemIterator.hasNext()){
//                    FileItemStream itemStream = itemIterator.next();
//                    //是否是一般的表单内容
//                    if(itemStream.isFormField()){
//                        //读取表单的值
//                        String value = Streams.asString(itemStream.openStream(),"UTF-8");
//                        //对表单的名称进行判断
//                        switch (itemStream.getFieldName()){
//                            case "name": name = value;break;
//                            case "age": age = value;break;
//                            case "gender": gender = value;break;
//                            case "color": color = value;break;
//                            case "price": price = value;break;
//                            case "typeId": typeId = value;break;
//                        }
//                    }else{
//                        //如果是文件域
//                        if("image".equals(itemStream.getFieldName())){
//                            //获得文件名
//                            image = itemStream.getName();
//                            image = System.currentTimeMillis() + FilenameUtils.getName(image);
//                            OutputStream out = new FileOutputStream(PATH + image);
//                            //实现文件保存
//                            Streams.copy(itemStream.openStream(), out, true);
//                        }
//                    }
//                }
//                try {
//                    petService.addPet(new Pet(0,name,Integer.valueOf(age),color,gender,Integer.valueOf(price),Integer.valueOf(typeId),null,image));
//                    System.out.println("添加成功！");
//                    resp.sendRedirect("/findPetsPage.do");
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            } catch (FileUploadException e) {
//                e.printStackTrace();
//            }
//        }
//        //调用service添加宠物
//
//    }
//
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        doGet(req,resp);
//    }
//}
